/*
 * MfClient.java
 *
 * Created on August 19, 2002, 12:27 AM
 */

package arc.mf.client.example;

import arc.mf.client.RemoteServer;
import arc.xml.XmlStringWriter;
import arc.xml.XmlDoc;
import arc.streams.LongInputStream;
import arc.streams.StreamCopy;

import java.util.Iterator;
import java.util.Collection;
import java.util.Properties;
import java.util.Vector;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import edu.monash.historyofadoption.MediafluxClient;
import edu.monash.historyofadoption.MediafluxConfig;

/**
 * 
 * @author jason lohrey
 * @version
 * 
 *          This sample client illustrates making calles to Mediaflux to create
 *          an asset, execute a query and rerieve assets in the query.
 */
public class SampleMfClient {

	public static void main(String[] args) throws IOException {

		// Get the inputStream
		InputStream inputStream = SampleMfClient.class.getClassLoader().getResourceAsStream("setup.properties");

		Properties properties = new Properties();

		// load the inputStream using the Properties
		properties.load(inputStream);
		// get the value of the property
		String host = properties.getProperty("mediaflux.server.host");
		int port = Integer.parseInt(properties.getProperty("mediaflux.server.port"));
		
		
		// RETRIEVE FORM POST VARS
		String domain = "mflux-dev";
		String user = "stevea";
		String password = "mfluxd3v";
		
		String basecollection = properties.getProperty("mediaflux.server.basecollection");

		boolean useSSL = Boolean.parseBoolean(properties.getProperty("mediaflux.server.useSSL"));
		boolean encrypt = Boolean.parseBoolean(properties.getProperty("mediaflux.server.encrypt"));

		try {
			MediafluxConfig mfConfig = new MediafluxConfig(host, port, domain, user, password, useSSL, encrypt, basecollection);
			MediafluxClient mfClient = new MediafluxClient(mfConfig);
			
			System.out.println("Collections under: " + mfConfig.getBasecollection());
			
			for(XmlDoc.Element namespaceElement : mfClient.getBaseCollectionList())
			{
				String namespace = mfClient.getNamespace(namespaceElement);
				System.out.println("Collection name: " + namespace);
				System.out.println("####");
				
				Collection<XmlDoc.Element> assetlist = mfClient.getAssetList(namespace);
				
				//mfClient.displayAssets(assetlist, System.out);
				
				System.out.println("");
			}

		} catch (Throwable t) {
			t.printStackTrace();
		}
	}


	private static boolean collectionExists(RemoteServer.Connection cxn, String namespace, String searchNamespace) throws Throwable {

		// Create the arguments document for the service. We'll ask for the
		// meta information for each asset to be returned - the default is to
		// simply return the asset identifier (which is equivalient to an
		// action of 'get-id'):
		XmlStringWriter w = new XmlStringWriter();
		w.add("namespace", namespace);

		// Execute the service 'asset.create' with the arguments and the (data)
		// input.
		// There is no (data) output for this service.
		XmlDoc.Element reply = cxn.execute("asset.namespace.list", w.document(), null, null);

		// Iterate through the results, and display each asset.
		Collection eles = reply.elements("namespace/namespace");
		if (eles == null) {
			return false;
		}

		Iterator it = eles.iterator();
		while (it.hasNext()) {
			XmlDoc.Element e = (XmlDoc.Element) it.next();
			if (e.value().equals(searchNamespace)) {
				return true;
			}
		}

		return false;
	}


}
